clear

*********
*** Replication code for
*** Dom, R., & Roger, L. 'Debt or alive: Burundi’s Fiscal Response to Economic Sanctions'.
*** International Studies Quarterly, 2020.
*********

* Settings / Parameters

global quarterly = 0 // 1: take sum over quarters ; 0: monthly data
global takelog = 0 // 1: take logs of variables ; 0: no logs
global ignorecrisis = 0 // 1: end sample after March (q1) 2015 ; 0: keep entire sample

* set working directory to folder "Replication-ISQ"
cd "C:\Users\user\Replication-ISQ"

* creating log file
local date: display %tdCCYYNNDD date(c(current_date),"DMY")
capture log close explore
log using "Explore_`date'.log", replace name(explore)


* Excel file needs manual correction: month = 11 is repeated in 2016, second should be 12
import excel "dataset_exp_stata.xlsx", sheet("Data") firstrow 
gen t = ym(year(date),month(date))
format %tm t
tsset t

* Implement settings (definition grants, sample, frequency, logs, ...)

gen grants = Grants_Cash

if $ignorecrisis == 1{
	keep if t <= 662 // before 2015m4; find numeric value of date with -di monthly("2015m3","YM")-
}


if $quarterly == 1{
	gen yq = qofd(dofm(t))
	replace t = yq
	gen quarter = quarter(date)
	
	gen ones = 1 // keep only quarters with 3 months available (=potentially drop first 1-2 months)
	bysort t: egen count = sum(ones) 
	keep if count == 3
	drop ones count
	
	collapse (first) year quarter (sum) grants Grants_Cash exp_cur_tot exp_tot exp_cap_net debt_int_total fisc_total NonFisc_Total_nogrants debt_int_centralbank  debt_int_banks debt_int_nonbanks, by(t)
	format %tq t
	tsset t
	
}


if $takelog == 1{
	replace grants = log(grants+1)
	replace exp_cur_tot = log(exp_cur_tot)
	replace exp_cap_net = log(exp_cap_net)
}

*********
*** DATA
*********

* generating total revenue variable
gen revenue = fisc_total+ NonFisc_Total_nogrants
label variable revenue "Total revenue"

* generating 2008 debt write-off dummy
if $quarterly == 0{
	gen cbDummy = ( year==2008 & month ==1)
}
if $quarterly == 1{
	gen cbDummy = ( year==2008 & quarter ==1)
}
label variable cbDummy "Debt write-off, 2008"

* generating 2012 debt write-off dummy
if $quarterly == 0{
	gen cbDummy2 = ( year==2012 & month ==1)
}
if $quarterly == 1{
	gen cbDummy2 = ( year==2012 & quarter ==1)
}
label variable cbDummy2 "Debt write-off, 2012"

* generating dummy for political crisis
gen pierre = 0 
if $quarterly == 0{
	replace  pierre =1 if t >= ym(2015,4)
}
if $quarterly == 1{
	replace  pierre =1 if t >= yq(2015,1)
}

label variable pierre "Crisis"

* generating trend variable
gen trend = _n
label variable trend "Trend"

* generating first difference of internal debt
gen Ddebt_int_total = D.debt_int_total
label variable Ddebt_int_total "Domestic borrowing"

* a few more labels
label variable Grants_Cash  "Grants"
label variable exp_tot  "Total expenditure"

* generating first difference of domestic debt to central bank
gen Ddebt_int_centralbank = d.debt_int_centralbank
label variable Ddebt_int_centralbank "DB central bank"

* generating first difference of domestic borrowing on financial market
gen debt_finmar = debt_int_banks + debt_int_nonbanks
gen Ddebt_finmar = d.debt_finmar
label variable Ddebt_finmar "DB fin. market"

* generating squared first difference of domestic borrowing on financial market
gen Ddebt_finmar_sq = Ddebt_finmar^2 
label variable Ddebt_finmar_sq "DB fin. market (sq.)"

*** Test data for stationarity

if $takelog == 1 {
	replace debt_int_total = log(debt_int_total)
}

*varsoc debt_int_total, maxlag(6) exog(trend)
dfuller debt_int_total, lags(2) //regress
*varsoc Ddebt_int_total, maxlag(6) exog(trend)
dfuller Ddebt_int_total, trend lags(1) //regress 
*varsoc Grants_Cash, maxlag(6) exog(trend)
dfuller Grants_Cash, lags(1) //regress
*varsoc revenue, maxlag(6) exog(trend)
dfuller revenue, lags(3) trend //regress
*varsoc exp_tot, maxlag(6) exog(trend)
dfuller exp_tot, lags(1) trend //regress

log close explore